import { Modal } from "antd-mobile";
import axios from "axios";
/** 创建axios实例 */
const service = axios.create({
  timeout: 15000 // 请求超时时间
});
/** request拦截器 */
service.interceptors.request.use(
  config => {
    return config;
  },
  error => {
    // 遇到错误或者超时
    Promise.reject(error);
  }
);
/**
 *  respone 拦截器
 */
service.interceptors.response.use(
  response => {
    /**
     * code为非20000是抛错 可结合自己业务进行修改
     */
    const res = response;
    if (res.status !== 200) {
      Modal.alert("请求警告", "服务器错误", [
        { text: "清除", onPress: () => console.log("cancel") },
        { text: "确定", onPress: () => console.log("ok") }
      ]);
      return Promise.reject(new Error("error"));
    } else {
      return response;
    }
  },
  error => {
    if (error.response.status === 504 || error.response.status === 404) {
      Modal.alert("请求警告", "服务器找不到", [
        { text: "清除", onPress: () => console.log("cancel") },
        { text: "确定", onPress: () => console.log("ok") }
      ]);
    } else {
      Modal.alert("请求警告", "服务器内部错误", [
        { text: "清除", onPress: () => console.log("cancel") },
        { text: "确定", onPress: () => console.log("ok") }
      ]);
    }
    return Promise.reject(error);
  }
);
export default service;
